package com.cat.greedyAlgorithm;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/max-difference-you-can-get-from-changing-an-integer/?envType=problem-list-v2&envId=GT3MFwwC
 * @create 2025/10/4 09:49
 * @since JDK17
 */

public class Solution53 {
    public int maxDiff(int num) {
        String s = String.valueOf(num), t = new String(s);
        int n = s.length(), min, max;
        char a = 0;
        if (s.charAt(0) != '1') {
            a = s.charAt(0);
            min = Integer.valueOf(s.replace(a, '1'));
        } else {
            for (var c : s.toCharArray()) {
                if (c != '0' && c != s.charAt(0)) {
                    a = c;
                    break;
                }
            }
            min = Integer.valueOf(s.replace(a, '0'));
        }
        for (var c : t.toCharArray()) {
            if (c != '9') {
                a = c;
                break;
            }
        }
        max = Integer.valueOf(t.replace(a, '9'));
        return max - min;
    }
}
